package com.wmx.wmxredis.controller;

import java.util.Date;

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.wmx.wmxredis.beans.Person;
import com.wmx.wmxredis.resultAPI.ResultCode;
import com.wmx.wmxredis.resultAPI.ResultData;
import com.wmx.wmxredis.utils.RedisCache;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 借助 {@link RedisCache} 工具类 操作 Redis
 *
 * @author wangMaoXiong
 */
@RestController
public class RedisCacheController {

    private static final String cacheKeyPrefix = "person#";

    @Resource
    private RedisCache redisCache;

    /**
     * 设置缓存：http://localhost:8080/redis2/save?id=1000&name=张三
     *
     * @return
     */
    @GetMapping("redis2/save")
    public ResultData<Object> save(@RequestParam Integer id,
                                   @RequestParam String name) {
        Person person = new Person();
        person.setId(id);
        person.setName(name);
        person.setBirthday(new Date());

        redisCache.setCacheObject(cacheKeyPrefix + "id#" + id, person);
        return ResultData.success("缓存成功");
    }

    /**
     * 设置缓存：http://localhost:8080/redis2/saveList?menuId=uuu100
     *
     * @return
     */
    @GetMapping("redis2/saveList")
    public ResultData<Object> saveList(@RequestParam String menuId) {
        String cacheKey = cacheKeyPrefix + "menuId#" + menuId;
        int size = 10;
        List<Person> persons = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            Person person = new Person();
            person.setId(1);
            person.setName("张" + i + "丰");
            person.setBirthday(new Date());
            persons.add(person);
        }
        redisCache.setCacheObject(cacheKey, persons);
        return ResultData.success("缓存成功");
    }

    /**
     * 查询缓存：http://localhost:8080/redis2/get?id=1000
     *
     * @param id
     * @return
     */
    @GetMapping("redis2/get")
    public ResultData getRedisCache(@RequestParam Integer id) {
        Person person = redisCache.getCacheObject(cacheKeyPrefix + "id#" + id);
        return ResultData.success(person);
    }

    /**
     * 查询缓存：http://localhost:8080/redis2/get2?menuId=uuu100
     *
     * @param menuId
     * @return
     */
    @GetMapping("redis2/get2")
    public ResultData getRedisCache2(@RequestParam String menuId) {
        String cacheKey = cacheKeyPrefix + "menuId#" + menuId;
        List<Person> persons = redisCache.getCacheObject(cacheKey);
        return ResultData.success(persons);
    }

}

